#include "SignEvent.h"
#include "GameSingleton.h"
#include "Log.h"
#include "BedWars.h"
#include "Components/ComponentPlayer.h"
#include "Components/ComponentClothing.h"
#include "Player/PlayerData.h"
#include "Network/PackageManager.h"
#include "Network/Packages/MessagePackage.h"
#include "SubsystemBedWars.h"
#include "Components/ComponentHealth.h"


using namespace PocketSurvival;

namespace BedWars
{
    bool SignEvent::playerTouchSign(std::shared_ptr<PlayerData> playerData, const Point3 &point)
    {
        if(m_bedWarsCore->isEditMode == false)
        {
            BedWarsStatue bedWarsStataue = m_bedWarsCore->subsystemBedWars->mainStatue;

            // 已经加入队伍的玩家是不允许重复加入的，需要先退出后才能加入
            if(m_bedWarsCore->subsystemBedWars->getEntityTeam(playerData->entity) != TeamType::None)
            {
                std::shared_ptr<MessagePackage> package = std::make_shared<MessagePackage>("你已经有加入队伍了，可以使用 /exit 退出后重新选择队伍");
                package->client = playerData->client;
                package->sendType = PackageSendType::TARGET;
                GameSingleton::packageManager->addSendPackageToQueue(package);
                return false;
            }

            static const std::string teamText[4] = {
                "欢迎加入红队",
                "欢迎加入蓝队",
                "欢迎加入绿队，你的强大来源于你这身绿色。\n你的帽子真好看! <em>34</em>",
                "欢迎加入紫队"
            };


            bool canJoinMaxPlayerTeam = false;
            TeamType maxPlayerTeam = TeamType::Blue;
            {
                uint8_t maxPlayerCount = 0;
                uint8_t secondMaxPlayerCount = 0;
                for (uint8_t index = 0; index < 4; index++)
                {
                    TeamType teamType = (TeamType)index;
                    uint8_t num = m_bedWarsCore->subsystemBedWars->getTeamPlayerCount(teamType);
                    if(num > maxPlayerCount)
                    {
                        secondMaxPlayerCount = maxPlayerCount;
                        maxPlayerCount = num;
                        maxPlayerTeam = teamType;
                    }
                    else if(num > secondMaxPlayerCount)
                    {
                        secondMaxPlayerCount = num;
                    }
                }
                if(maxPlayerCount - secondMaxPlayerCount < 1)
                {
                    canJoinMaxPlayerTeam = true;
                }
            }

            for(uint8_t teamIndex = 0; teamIndex < 4; teamIndex++)
            {
                TeamType teamType = (TeamType)teamIndex;
                if(point == m_bedWarsCore->teamData[teamIndex].joinSignPoint)
                {
                    // 结算过程中是不允许加入游戏的，而其它过程，除了坟被挖了，不然是可以中途加入的
                    if(bedWarsStataue != BedWarsStatue::Ending)
                    {
                        if(m_bedWarsCore->subsystemBedWars->getTeamPlayerCount(teamType) >= m_bedWarsCore->teamPlayerCount)
                        {
                            std::shared_ptr<MessagePackage> package = std::make_shared<MessagePackage>("该队伍人数已满，无法加入");
                            package->client = playerData->client;
                            package->sendType = PackageSendType::TARGET;
                            GameSingleton::packageManager->addSendPackageToQueue(package);
                            return false;
                        }
                        if(canJoinMaxPlayerTeam == false && teamType == maxPlayerTeam)
                        {
                            std::shared_ptr<MessagePackage> package = std::make_shared<MessagePackage>("人数不平衡，无法进入该队伍");
                            package->client = playerData->client;
                            package->sendType = PackageSendType::TARGET;
                            GameSingleton::packageManager->addSendPackageToQueue(package);
                            return false;
                        }
                        if(bedWarsStataue == BedWarsStatue::Prepare )
                        {
                            m_bedWarsCore->subsystemBedWars->resetPlayer(playerData->componentPlayer);
                            m_bedWarsCore->subsystemBedWars->setTeamClothes(playerData->componentPlayer, teamType);
                            m_bedWarsCore->subsystemBedWars->joinTeam(playerData->entity, teamType);
                        }
                        else if(bedWarsStataue == BedWarsStatue::GameRunning || bedWarsStataue == BedWarsStatue::Countdown)
                        {
                            // 家都没了，肯定是不能再加入的了
                            if(m_bedWarsCore->subsystemBedWars->getTeamHealth(teamType) == 0 || m_bedWarsCore->subsystemBedWars->canRestart == false)
                            {
                                std::shared_ptr<MessagePackage> package = std::make_shared<MessagePackage>("该队伍已经无法加入，请等待下一局");
                                package->client = playerData->client;
                                package->sendType = PackageSendType::TARGET;
                                GameSingleton::packageManager->addSendPackageToQueue(package);
                                return false;
                            }
                            playerData->componentPlayer->componentHealth->invincibleTime = 1.5f;
                            m_bedWarsCore->subsystemBedWars->resetPlayer(playerData->componentPlayer);
                            m_bedWarsCore->subsystemBedWars->setTeamClothes(playerData->componentPlayer, teamType);
                            m_bedWarsCore->subsystemBedWars->joinTeam(playerData->entity, teamType);
                            m_bedWarsCore->subsystemBedWars->toTeamHomePosition(playerData->componentPlayer);

                            std::shared_ptr<IPackage> package = std::make_shared<MessagePackage>("游戏已经开始", "挖掉其它队伍的石碑，没有石碑的队伍不能复活玩家\n箱子是商店，上一行是商品，下一行是价格\n买物品的时候直接拖动商品即可\n铜锭出生点就有，铁锭附近的小岛有，钻石中间的大岛有", 5.0f, 0.0f);
                            package->sendType = PackageSendType::TARGET;
                            package->client = playerData->client;
                            GameSingleton::packageManager->addSendPackageToQueue(package);

                        }
                        std::shared_ptr<MessagePackage> package = std::make_shared<MessagePackage>(teamText[teamIndex]);
                        package->client = playerData->client;
                        package->sendType = PackageSendType::TARGET;
                        GameSingleton::packageManager->addSendPackageToQueue(package);
                    }
                    else
                    {
                            std::shared_ptr<MessagePackage> package = std::make_shared<MessagePackage>("游戏已经结束，请稍等一会");
                            package->client = playerData->client;
                            package->sendType = PackageSendType::TARGET;
                            GameSingleton::packageManager->addSendPackageToQueue(package);
                    }
                    return false;
                }
            }
            return false;
        }
        return true;
    }


    bool SignEvent::playerEditSign(std::shared_ptr<PlayerData> playerData, SignTextData &signTextData)
    {

        // 编辑模式下才能修改牌子
        // 每个队伍有一个特定的加入牌子
        // 在牌子的第一行写对应队伍的颜色，就会转换成加入牌子

        if(m_bedWarsCore->isEditMode == true)
        {
            if(signTextData.lines[0] == "red")
            {
                m_bedWarsCore->teamData[0].joinSignPoint = signTextData.point;
                signTextData.colors[0] = FastColor4::Red;
                signTextData.colors[1] = FastColor4::Red;
                signTextData.colors[2] = FastColor4::Red;
                signTextData.colors[3] = FastColor4::Red;
                signTextData.lines[0] = "[点我加入红队]";

            }
            else if(signTextData.lines[0] == "blue")
            {
                m_bedWarsCore->teamData[1].joinSignPoint = signTextData.point;
                signTextData.colors[0] = FastColor4::Blue;
                signTextData.colors[1] = FastColor4::Blue;
                signTextData.colors[2] = FastColor4::Blue;
                signTextData.colors[3] = FastColor4::Blue;
                signTextData.lines[0] = "[点我加入蓝队]";

            }
            else if(signTextData.lines[0] == "green")
            {
                m_bedWarsCore->teamData[2].joinSignPoint = signTextData.point;
                signTextData.colors[0] = FastColor4::Green;
                signTextData.colors[1] = FastColor4::Green;
                signTextData.colors[2] = FastColor4::Green;
                signTextData.colors[3] = FastColor4::Green;
                signTextData.lines[0] = "[点我加入绿队]";


            }
            else if(signTextData.lines[0] == "purple")
            {
                m_bedWarsCore->teamData[3].joinSignPoint = signTextData.point;
                signTextData.colors[0] = FastColor4::Purple;
                signTextData.colors[1] = FastColor4::Purple;
                signTextData.colors[2] = FastColor4::Purple;
                signTextData.colors[3] = FastColor4::Purple;
                signTextData.lines[0] = "[点我加入紫队]";


            }
            return true;
        }
        return false;
    }
} // namespace BedWars
